package com.hx.ui;

import com.hx.cmd.CommandCenter;

import android.widget.Button;
import android.widget.ImageView;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;

public class MainWidget implements View.OnClickListener , View.OnTouchListener{
	protected Button ovalBtn;
	protected Button rectBtn;
	protected Button lineBtn;
	protected ImageView canvas;
	protected Activity activity;

	public MainWidget(Activity activity,Button ovalBtn,Button rectBtn,Button lineBtn,ImageView canvas){
		this.ovalBtn = ovalBtn;
		ovalBtn.setOnClickListener(this);
		this.rectBtn = rectBtn;
		rectBtn.setOnClickListener(this);
		this.lineBtn = lineBtn;
		lineBtn.setOnClickListener(this);
		this.canvas = canvas;
		canvas.setOnTouchListener(this);
		this.activity = activity;
	}

	public void onClick(View v) {
		if(v == ovalBtn)
			CommandCenter.getInstance().setDrawMode(CommandCenter.OVALMODE);
		else if(v == rectBtn)
			CommandCenter.getInstance().setDrawMode(CommandCenter.RECTMODE);
		else if(v == lineBtn)
			CommandCenter.getInstance().setDrawMode(CommandCenter.LINEMODE);
	}

	public boolean onTouch(View v, MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		
		int action = event.getAction();
		if(action == MotionEvent.ACTION_DOWN){
			CommandCenter.getInstance().createShape(x,y);
		}
		else if(action == MotionEvent.ACTION_MOVE){
			CommandCenter.getInstance().modifyShape(x,y);
		}
		else if(action == MotionEvent.ACTION_UP){
			CommandCenter.getInstance().finishShape(x,y);
		}
		canvas.invalidate();
		return true;
	}
}


